package com.tong.embeddedvr.controller;

import com.tong.embeddedvr.dto.*;
import com.tong.embeddedvr.entity.UserEntity;
import com.tong.embeddedvr.service.impl.HomeworkServiceImpl;
import com.tong.embeddedvr.utils.CommonUtil;
import org.apache.http.HttpResponse;
import org.apache.ibatis.annotations.ResultMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * TODO
 *
 * @author gcl
 * @version 1.0
 * @Description
 * @date 2021/1/1 11:04
 */

@CrossOrigin
@RestController
@RequestMapping("/homework")
public class HomeworkController {

    @Autowired
    HomeworkServiceImpl homeworkService;

    private Logger logger = LoggerFactory.getLogger(HomeworkController.class);

    @RequestMapping("/showtablebycollectionid")
    String showTable(TableReqDTO tableReqDTO, CollectionDTO collectionDTO){
        String res = "请联系管理员";
        try {
            TableRspDTO tableRspDTO = homeworkService.showTable(tableReqDTO, collectionDTO);
            res = CommonUtil.ajaxReturn(tableRspDTO);
        } catch (Exception e) {
            CommonUtil.ajaxReturn("请求失败,请稍后重试!");
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/showtablebyuserid")
    String showTable(TableReqDTO tableReqDTO, UserInputDTO userInputDTO){
        String res = "请联系管理员";
        try {
            TableRspDTO tableRspDTO = homeworkService.showTable(tableReqDTO, userInputDTO);
            res = CommonUtil.ajaxReturn(tableRspDTO);
        } catch (Exception e) {
            e.printStackTrace();
            res = CommonUtil.ajaxReturn(400,"请求出错,请联系管理员!");
        }
        return res;
    }

    @RequestMapping("/showhomework")
    String showHomework(HomeworkDTO homeworkDTO){
        String res = "请联系管理员";
        try {
            HomeworkDTO homeworkInfo = homeworkService.showHomework(homeworkDTO);
            res = CommonUtil.ajaxReturn(homeworkInfo);
        } catch (Exception e) {
            res = CommonUtil.ajaxReturn(400, "请求失败,请稍后重试");
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/showhomeworkbyuseridandcollectionid")
    String showHomeworkByUserIdAndCollectionId(HomeworkDTO homeworkDTO){
        return homeworkService.showHomeworkByUserIdAndCollectionId(homeworkDTO);
    }

    @RequestMapping("/evaluate")
    String homeworkEvaluate(HomeworkDTO homeworkDTO){
        String res = "请联系管理员";
        try{
            Integer num = homeworkService.evaluate(homeworkDTO);
            if(num==1){
                res = CommonUtil.ajaxReturn("评价成功!");
            }else{
                res = CommonUtil.ajaxReturn(400,"更新失败,请确保评价内容正确!");
            }
        }catch (Exception e){
            e.printStackTrace();
            res = CommonUtil.ajaxReturn(400,"更新失败,请稍后重试");
        }
        return res;
    }

    @RequestMapping("/submithomework")
    String submitHomework(HomeworkDTO homeworkDTO,CollectionDTO collectionDTO){
        String res = "请联系管理员";
        try {
            Integer num = homeworkService.submitHomework(homeworkDTO, collectionDTO);
            if(num!=0){
                res = CommonUtil.ajaxReturn(num);
            }else{
                res = CommonUtil.ajaxReturn(400,"提交失败,请检查作业是否已提交并检查数据是否有效!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            res = CommonUtil.ajaxReturn(400,"请稍后尝试!");
        }
        return res;
    }

    //@RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象
    //批量上传CommonsMultipartFile则为数组即可
    @RequestMapping("/fileUpload")
    public String fileUpload(@RequestParam("file") CommonsMultipartFile file , HttpServletRequest request){
        String res;
        try{
            res = homeworkService.fileUpload(request,file);
        }catch(Exception e){
            res = CommonUtil.ajaxReturn(400,"文件上传失败!");
            logger.error("错误信息:[{}]",e.getMessage());
        }
        return res;
    }

    @RequestMapping("/fileDownload")
    public void fileDownload(HttpServletResponse resp,HomeworkDTO homeworkDTO) {
        homeworkService.fileDownload(resp,homeworkDTO);
    }

    @RequestMapping("/delhomework")
    public String delHomework(HomeworkDTO homeworkDTO) {
        return homeworkService.delHomework(homeworkDTO);
    }

    @RequestMapping("/test")
    void test(HttpServletResponse resp){
        String filePath = "C:\\Users\\gcl\\Desktop\\test.md";
    }

}
